package leetcode.N1_N100;

import leetcode.comm.ListNode;

public class N19 {

    public static void main(String[] args) {

        ListNode head = ListNode.generate(1,2,3,4,5,6,7);
        System.out.println(removeNthFromEnd(head, 7));

    }

    private static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode stay = head;
        ListNode move = head;
        for (int i = 0; i < n + 1; i++) {
            if (move == null) {
                return head.next;
            }
            move = move.next;
        }
        for (ListNode node = move; node != null; node = node.next) {
            stay = stay.next;
        }
        stay.next = stay.next.next;
        return head;
    }

}
